package com.hspedu.tankgame3Exercise;

/**
 * @author DL5O
 * @version 1.0
 */
public class Shot implements Runnable {
    private int x;
    private int y;
    private int speed;//速度
    private int direct;//方向
    boolean isLive = true;

    public Shot(int x, int y, int speed, int direct) {
        this.x = x;
        this.y = y;
        this.speed = speed;
        this.direct = direct;
    }

    public int getX() {
        return x;
    }

    public void setX(int x) {
        this.x = x;
    }

    public int getY() {
        return y;
    }

    public void setY(int y) {
        this.y = y;
    }

    public int getSpeed() {
        return speed;
    }

    public void setSpeed(int speed) {
        this.speed = speed;
    }

    public int getDirect() {
        return direct;
    }

    public void setDirect(int direct) {
        this.direct = direct;
    }

    //发射
    @Override
    public void run() {
        while (true) {
            try {
                Thread.sleep(50);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

            switch (direct) {
                case 0://向上
                    y -= speed;
                    break;
                case 1://向右
                    x += speed;
                    break;
                case 2://向下
                    y += speed;
                    break;
                case 3://向左
                    x -= speed;
                    break;
                default:
                    break;
            }
            System.out.println("子弹发射.. x=" + x + ",y=" + y);

            //如果子弹到达边界，或者子弹生命周期结束，就退出该线程
            if (!(x>=0 && x<=1000 && y>=0 && y<=750 && isLive)){
                isLive = false;
                System.out.println("子弹线程退出");
                break;
            }
        }
    }
}
